<?php

namespace Home\Controller;
class ReturnOrderGoodsController extends CommonController
{
    //图片上传
    public function uploda_image()
    {
        $this->display('index');
    }

    //图片上传
    public function upload_image()
    {
        //控制上传数量，5张
        $arr_count = count($_FILES['image']['name']);
        if ($arr_count > 5) {
            json_error('最多只能上传5张图片');
        }

        $dir = 'return_goods';

        $upload = new \Think\Upload();// 实例化上传类

        $image_dir = ROOT_PATH . 'Uploads/image/' . $dir;

        if (!is_dir($image_dir)) {
            @mkdir($image_dir, 0777);
        }

        $upload->autoSub = false;
        $upload->maxSize = 5242880;// 设置附件上传大小 5M = 5*1024*1024
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $image_dir . '/';

        $info = $upload->upload();

        if (!$info) {
            json_error($upload->getError());

        } else {// 上传成功

            foreach ($info as $k => $v) {
                $filename = $dir . '/' . $v['savename'];
                $data['image'][] = $filename;
            }
            json_data($data);

        }
    }

    //填写申请售后页的购买商品显示数据
    public function afterSale_show()
    {
        //session 用户信息
        $member_data = session('member_data');
        $member_id = $member_data['accountId'];
        $order_id = I('order_id');
        //$order_id = 130;

        if (empty($order_id)) {
            json_error('参数错误');
        }

        $order_model = D('ApiOrder');
        $where = array(
            'member_id' => $member_id,
            'order_id' => $order_id,
        );

        //查询订单
        $finds = 'order_id,goods_id,quantity,order_num_alias,price,total';
        $order_data = $order_model->getOrderInfo($where, $finds);
        if (empty($order_data)) {
            json_error('数据有误');
        }

        //查询商品信息
        $goods_model = D('Goods');
        //商品ig
        $goods_id = $order_data['goods_id'];
        $order_id = $order_data['order_id'];

        $finds = 'goods_id,name,image,before_discount';
        $g_where = array(
            'goods_id' => $goods_id,
        );

        //查询商品
        $goods_data = $goods_model->goodsInfo($g_where, $finds);
        $order_data['name'] = $goods_data['name'];
        $order_data['image'] = $goods_data['image'];

        //查询商品的订单商品规格
        $orderOption_model = D('OrderOption');
        $orderOptio_w = array(
            'order_id' => $order_id,
        );

        $orderOption_data = $orderOption_model->orderOptioList($orderOptio_w);
        if (!empty($orderOption_data)) {
            $option_data = array();

            //查询订单商品
            foreach ($orderOption_data as $k => $v) {
                $option_list = M()->query('select * from ' . C('DB_PREFIX') . 'order_goods where order_goods_id=' . $v['order_goods_id'] . ' and order_id=' . $v['order_id']);
                $option_data[] = $option_list[0];
            }
        }

        //组装商品选项规格
        foreach ($option_data as $k => $v) {
            foreach ($orderOption_data as $kk => $vv) {
                if ($v['order_goods_id'] == $vv['order_goods_id'] && $v['order_id'] == $vv['order_id']) {
                    $option_data[$k]['option_data'][] = $vv['name'] . ' : ' . $vv['value'];
                }

            }

        }

        //组装商品选项规格
        foreach ($option_data as $kk => $vv) {
            if ($order_data['goods_id'] == $vv['goods_id']) {
                $order_data['option_data'] = $vv['option_data'];
            }
        }

        json_data(array('data_list' => $order_data));
    }


    //提交退换货申请单
    public function ReturnOrderGoods()
    {
        //session 用户信息
        $member_data = session('member_data');
        $user_id = $member_data['accountId'];
        $user_name = $member_data['realName'];
        $order_id = I('order_id');
        //$order_id = 130;
        $goods_id = I('goods_id');
        //$goods_id = 1;
        $order_num = I('order_num_alias');
        //$order_num = '2017122951485610';
        $price = I('total');
        //$price = '3042.00';
        $number = I('number');
        //$number = 1;
        $type = I('type');
        //$type = 1;
        $tuihuo_case = I('tuihuo_case');
        //$tuihuo_case = '质量不行';
        $explain = I('explain');
        //$explain = '质量不行,质量不行,质量不行,质量不行,质量不行,质量不行,质量不行,质量不行';
        $voucher = I('voucher');
        /*$voucher = [
            "return_goods/5a53226380889.jpg",
            "return_goods/5a53226381441.png",
            "return_goods/5a53226381ff9.png",
            "return_goods/5a532263827c9.png"
        ];*/
        $voucher = serialize($voucher);

        if (empty($order_id)) {
            json_error('参数错误');
        }
        $model = D('OrderReturnGoods');

        $where = array(
            'user_id' => $user_id,
            'order_id' => $order_id,
            'status' => '7',
        );
        //查询提交了售后，没有审核的售后单
        $r = $model->getReturnInfo($where);
        if (!empty($r)) {
            json_error('该订单售后我们正在审核，请勿重复提交');
        }

        //统计同一个订单提交售后的次数
        $where['status'] = '8';
        $count = $model->countReturn($where);

        if ($count >= '3') {
            json_error('您已经提交了三次售后单，不能再提交了');
        }

        $data = array(
            'user_id' => $user_id,
            'user_name' => $user_name,
            'order_id' => $order_id,
            'goods_id' => $goods_id,
            'order_num' => $order_num,
            'price' => $price,
            'type' => $type,
            'tuihuo_case' => $tuihuo_case,
            'explain' => $explain,
            'voucher' => $voucher,
            'number' => $number,
            'return_num' => build_return_no(),
            'status' => 7,
            'create_time' => time(),
        );


        $re = $model->addReturn($data);
        if ($re) {

            /* Evan  2018-1-15
             * //修改订单表的状态
            $order_model = D('ApiOrder');
            $where = array(
                'member_id' => $user_id,
                'order_id' => $order_id,
            );
            $up = array(
                'order_status_id' => '7',
            );
            $order_model->editOrder($where, $up);*/

            //记录日志
            storage_user_action($user_id, $user_name, C('FRONTEND_USER'), '提交了售后订单，订单号： ' . $data['order_num'] . ' 售后单号：' . $data['return_num']);

            json_data('提交成功');
        } else {
            json_error('提交失败');
        }
    }

    //售后单列表
    public function after_sale_list()
    {
        //session 用户信息
        $member_data = session('member_data');
        $member_id = $member_data['accountId'];

        $where = array(
            'user_id' => $member_id,
        );

        $model = D('OrderReturnGoods');


        //总数、分页
        $count = $model->countReturn($where);


        //接收post转换get
        $_GET['page'] = $_POST['page'];

        //分页数
        $page = $_POST['page'];

        //一页显示的条数
        $num = 10;

        //返回是否有下页数据
        $load = api_page($page, $num, $count);
        $finds = 'return_num,order_id,goods_id,price,order_num,status,create_time';
        $order = 'status asc';
        $order_list = $model->getReturnList($where, $finds, $order, '', $page, $num);

        if (!empty($order_list)) {

            //查询商品信息
            $goods_model = D('Goods');
            //商品ig
            $goods_id = array();
            $order_id = array();
            foreach ($order_list as $k => $v) {
                $goods_id[] = $v['goods_id'];
                $order_id[] = $v['order_id'];
            }

            $goods_id = array_unique($goods_id);

            $finds = 'goods_id,name,image';
            $g_where = array(
                'goods_id' => array('in', $goods_id)
            );

            //查询商品
            $goods_list = $goods_model->goodsList($g_where, $finds, '', '10');

            //查询商品的订单商品规格
            $orderOption_model = D('OrderOption');
            $orderOptio_w = array(
                'order_id' => array('in', $order_id)
            );

            $orderOption_data = $orderOption_model->orderOptioList($orderOptio_w);
            if (!empty($orderOption_data)) {
                $option_data = array();
                foreach ($orderOption_data as $k => $v) {
                    $option_list = M()->query('select * from ' . C('DB_PREFIX') . 'order_goods where order_goods_id=' . $v['order_goods_id'] . ' and order_id=' . $v['order_id']);
                    $option_data[] = $option_list[0];
                }
            }

            //组装商品选项规格
            foreach ($option_data as $k => $v) {
                foreach ($orderOption_data as $kk => $vv) {

                    if ($v['order_goods_id'] == $vv['order_goods_id'] && $v['order_id'] == $vv['order_id']) {
                        $option_data[$k]['option_data'][] = $vv['name'] . ' : ' . $vv['value'];
                    }

                }

            }

            //组装商品信息
            foreach ($order_list as $k => $v) {

                foreach ($goods_list as $kk => $vv) {
                    if ($v['goods_id'] == $vv['goods_id']) {
                        $order_list[$k]['name'] = $vv['name'];
                        $order_list[$k]['image'] = $vv['image'];
                    }
                }
                $order_list[$k]['return_goods_status'] = order_stute_text($v['status']);
                if ($v['status'] == '9') {
                    $order_list[$k]['return_goods_status'] = '填写发货单';
                }
                $order_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }

            //组装商品选项规格
            foreach ($order_list as $k => $v) {
                foreach ($option_data as $kk => $vv) {
                    if ($v['goods_id'] == $vv['goods_id']) {
                        $order_list[$k]['option_data'] = $vv['option_data'];
                    } else {
                        $order_list[$k]['option_data'] = array();
                    }
                }

            }

        } else {
            $order_list = array();
        }
        json_data(array('load' => $load, 'data_list' => $order_list));
    }


    //售后单详情
    public function after_salet_details()
    {
        //session 用户信息
        $member_data = session('member_data');
        $member_id = $member_data['accountId'];
        $id = I('id');

        if (empty($id)) {
            json_error('参数不能为空');
        }

        $where = array(
            'user_id' => $member_id,
            'id' => $id,
        );

        $model = D('OrderReturnGoods');

        $r_order = $model->getReturnInfo($where);

        if (!empty($r_order)) {

            //查询商品信息
            $goods_model = D('Goods');
            //商品ig
            $goods_id = $r_order['goods_id'];
            $order_id = $r_order['order_id'];

            $finds = 'goods_id,name,image';
            $g_where = array(
                'goods_id' => $goods_id,
            );

            //查询商品
            $goods = $goods_model->goodsInfo($g_where, $finds);

            $r_order['name'] = $goods['name'];
            $r_order['image'] = $goods['image'];

            //查询商品的订单商品规格
            $orderOption_model = D('OrderOption');
            $orderOptio_w = array(
                'order_id' => $order_id,
            );

            $orderOption_data = $orderOption_model->orderOptioList($orderOptio_w);

            if (!empty($orderOption_data)) {
                $option_data = array();
                foreach ($orderOption_data as $k => $v) {
                    $option_list = M()->query('select * from ' . C('DB_PREFIX') . 'order_goods where order_goods_id=' . $v['order_goods_id'] . ' and order_id=' . $v['order_id']);
                    $option_data[] = $option_list[0];
                }
            }


            //组装商品选项规格
            foreach ($option_data as $k => $v) {
                foreach ($orderOption_data as $kk => $vv) {
                    if ($v['order_goods_id'] == $vv['order_goods_id'] && $v['order_id'] == $vv['order_id']) {
                        $option_data[$k]['option_data'][] = $vv['name'] . ' : ' . $vv['value'];
                    }

                }

            }

            $r_order['status_text'] = order_stute_text($r_order['status']);
            if ($r_order['status'] == '9') {
                $r_order['status_text'] = order_stute_text($r_order['status']);
                $r_order['status_text'] = '填写发货单';
            }
            if ($r_order['type'] == '1') {
                $r_order['type_text'] = '退货退款';
            } else {
                $r_order['type_text'] = '换货';
            }

            if (!empty($r_order['create_time'])) {
                $r_order['create_time'] = date('Y-m-d H:i:s', $r_order['create_time']);
            }
            if (!empty($r_order['revocation_time'])) {
                $r_order['revocation_time'] = date('Y-m-d H:i:s', $r_order['revocation_time']);
            }
            if (!empty($r_order['update_time'])) {
                $r_order['update_time'] = date('Y-m-d H:i:s', $r_order['update_time']);
            }

            $r_order['voucher'] = unserialize($r_order['voucher']);
            //组装商品选项规格
            foreach ($option_data as $k => $v) {
                if ($r_order['goods_id'] == $v['goods_id']) {
                    $r_order['option_data'] = $v['option_data'];
                } else {
                    $r_order['option_data'] = array();
                }
            }

            //查询寄回收货地址
            $finds = 'address_id,member_id,name,telephone,address';
            $address = M('address')->where(array('member_id' => '-1'))->field($finds)->find();
            $r_order['address_data'] = $address;
        } else {
            $r_order = array();
        }
        json_data(array('data_list' => $r_order));
    }

    //取消售后单号
    public function cancel_afterSalet()
    {
        //session 用户信息
        $member_data = session('member_data');
        $member_id = $member_data['accountId'];
        $id = I('id');

        if (empty($id)) {
            json_error('参数不能为空');
        }

        $where = array(
            'user_id' => $member_id,
            'id' => $id,
        );

        $model = D('OrderReturnGoods');

        $r_order = $model->getReturnInfo($where);

        if ($r_order['status'] == '12') {
            json_error('该售后单在处理中。。。，不能取消');
        }
        $up = array(
            'status' => '13',
        );
        $re = $model->editReturn($where, $up);

        if ($re) {
            json_data('取消成功');
        } else {
            json_error('取消失败');
        }
    }


    //售后单填写发货单号
    public function import_express()
    {
        //session 用户信息
        $member_data = session('member_data');
        $member_id = $member_data['accountId'];
        $id = I('id');
        $express_id = I('express_id');
        $express_name = I('express_name');

        if (empty($id)) {
            json_error('参数不能为空');
        }
        if (empty($express_id)) {
            json_error('请输入快递单号');
        }
        if (empty($express_name)) {
            json_error('请输入快递公司名称');
        }

        $where = array(
            'user_id' => $member_id,
            'id' => $id,
        );

        $model = D('OrderReturnGoods');

        $r_order = $model->getReturnInfo($where);
        if (empty($r_order)) {
            json_error('参数错误');
        }

        if (!empty($r_order['express_id'])) {
            json_error('该售后单已经填写了发货信息');
        }
        $up = array(
            'express_id' => $express_id,
            'express_name' => $express_name,
        );
        $re = $model->editReturn($where, $up);

        if ($re) {
            json_data('填写成功');
        } else {
            json_error('填写失败');
        }
    }


}

?>